<?php
class Log {
    const LEVEL_DEBUG = 1;
    const LEVEL_INFO = 2;
    const LEVEL_ERROR = 3;

    static $logfile;
    static $loglevel = Log::LEVEL_DEBUG;
    static $threadId = "";

    static function openLog() {
        $today = date("Y_m_d");
        self::$logfile = fopen("logs/impex_$today.log", "a");
        self::$threadId = $_SESSION['sid'];
    }

    static function setLogLevel($loglevel) {
        self::$loglevel = $loglevel;
    }

    static function closeLog() {
        fclose(self::$logfile);
    }

    static function debug($msg) {
        if (self::$loglevel == Log::LEVEL_INFO || self::$loglevel == Log::LEVEL_ERROR) { return; }
        $dt = getdate();
        $timestamp = $dt['year'] . "." . $dt['mon'] . "." . $dt['mday'] . " " . $dt['hours'] . ":" . $dt['minutes'] . ":" . $dt['seconds'];
        fputs(self::$logfile, "$timestamp " . self::$threadId . " [DEBUG] $msg\r\n");
    }
    
    static function info($msg) {
        if (self::$loglevel == Log::LEVEL_ERROR) { return; }
        $dt = getdate();
        $timestamp = $dt['year'] . "." . $dt['mon'] . "." . $dt['mday'] . " " . $dt['hours'] . ":" . $dt['minutes'] . ":" . $dt['seconds'];
        fputs(self::$logfile, "$timestamp " . self::$threadId . " [INFO] $msg\r\n");
    }
    
    static function error($msg) {
        $dt = getdate();
        $timestamp = $dt['year'] . "." . $dt['mon'] . "." . $dt['mday'] . " " . $dt['hours'] . ":" . $dt['minutes'] . ":" . $dt['seconds'];
        fputs(self::$logfile, "$timestamp " . self::$threadId . " [ERROR] $msg\r\n");
    }
}

?>
